02. Getting Started

L6 A02 Getting Started

Download the Code for this Lesson

You have two options for the code in this lesson:

  • You can download a zip file of the starter code here
  • If you’re familiar with git, you can clone this repository. The master branch of the repository contains the solution code up to Concept 8: Summary & Next Exercise and the ‘start’ branch of the repository contains //TODO comments that you will implement as you work through the lesson. As you begin this lesson, make sure you are on the ‘start’ branch of the repository.

Add Firebase to the project

Step 1: Create a Firebase project

Before you can add Firebase to your Android app, you need to create a Firebase project to connect to your Android app.

  1. In the Firebase console, click Add project, then select or enter a Project name. You can name your project anything, but try to pick a name relevant to the app you’re building.
  2. Click Continue.
  3. You can skip setting up Google Analytics and chose the Not Right Now option.
  4. Click Create Project to finish setting up the Firebase project.

Step 2: Register your app with Firebase

Now that you have a Firebase project, you can add your Android app to it.

  1. In the center of the Firebase console's project overview page, click the Android icon to launch the setup workflow.
  2. Enter your app's application ID in the Android package name field. Make sure you enter the ID your app is using, otherwise you cannot add or modify this value after you’ve registered your app with your Firebase project.
    • An application ID is sometimes referred to as a package name.
    • Find this application ID in your module (app-level) Gradle file, usually app/build.gradle (example ID: com.yourcompany.yourproject).
  3. Enter the debug signing certificate SHA-1. You can generate this key by entering the following command in your command line terminal
keytool -alias androiddebugkey -keystore ~/.android/debug.keystore -list -v -storepass android
  1. Click to Register app.

Step 3: Add the Firebase configuration file to your project

Add the Firebase Android configuration file to your app:

  1. Click Download google-services.json to obtain your Firebase Android config file (google-services.json).
    • You can download your Firebase Android config file again at any time.
    • Make sure the config file is not appended with additional characters and should only be named google-services.json.
  2. Move your config file into the module (app-level) directory of your app.

Step 4: Configure your Android project to enable Firebase products

  1. To enable Firebase products in your app, add the google-services plugin to your Gradle files.
    1. In your root-level (project-level) Gradle file (build.gradle), add rules to include the Google Services plugin. Check that you have Google's Maven repository, as well.

build.gradle

buildscript {

  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
  }

  dependencies {
    // ...

    // Add the following line:
    classpath 'com.google.gms:google-services:4.3.0'  // Google Services plugin
  }
}

allprojects {
  // ...

  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
    // ...
  }
}
  1. In your module (app-level) Gradle file (usually app/build.gradle), add a line to the bottom of the file.

app/build.gradle

apply plugin: 'com.android.application'

android {
  // ...
}

// Add the following line to the bottom of the file:
apply plugin: 'com.google.gms.google-services'  // Google Play services Gradle plugin

Add the Firebase dependency

In this lesson the main use case for integrating Firebase is to have a way to create and manage users. So far you’ve set up your app so that it can communicate with Firebase, but now you need to add a specific Firebase library that enables you to implement login.

  1. The firebase-auth SDK allows management of authenticated users of your application. Add the following dependency in your build.gradle (Module:app) file so that you can use the SDK in your app.

app/build.gradle

implementation 'com.firebaseui:firebase-ui-auth:5.0.0'
  1. Sync your project with gradle files.

To be sure that all dependencies are available to your app, you should sync your project with gradle files. Select File > Sync Project with Gradle Files in Android Studio, or from the toolbar.

Run the app

Once you’ve set up Firebase, run the app on an emulator or physical device to ensure that your environment has successfully been set up to start development. If successful, you should see the home screen display a fun Android fact and a login button on the top left corner. Tapping the login button doesn’t do anything just yet.